<template>
  <!-- 添加或修改付款申请单对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-row :gutter="10">
      <el-col :span="24">
            <el-card class="box-card">
              <el-descriptions class="margin-top " :contentStyle="CS" :label-style="LS" title="" :column="2" size="medium" border>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-user"></i>
                    供应商名称
                  </template>
                  {{form.supplierName}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-money"></i>
                    供应商银行
                  </template>
                  {{form.bankName}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-c-scale-to-original"></i>
                    供应商行号
                  </template>
                  {{form.bankNo}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-bank-card"></i>
                    供应商账号
                  </template>
                  {{form.accountNo}}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-bank-card"></i>
                    支付时间
                  </template>
                  {{ parseTime(form.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-bank-card"></i>
                    付款金额
                  </template>
                  {{form.money}}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-bank-card"></i>
                    状态
                  </template>
                  <dict-tag :options="dict.type.pay_record_status" :value="form.status"/>
                </el-descriptions-item>

                <el-descriptions-item>
                  <template slot="label">
                    <i class="el-icon-bank-card"></i>
                    付款凭证
                  </template>
                  <file-upload-show :value="form.attachment"></file-upload-show>
                </el-descriptions-item>
              </el-descriptions>
            </el-card>
      </el-col>
    </el-row>

    <el-table border v-loading="loading" :data="payApplyBuyDetailList" ref="table"
              show-summary :summary-method="getSummaries">
      <el-table-column type="selection" width="55" align="center" :selectable="selectable" />
      <el-table-column label="序号" type="index" align="center" width="55"></el-table-column>
      <el-table-column  label="采购单号" align="center" prop="params.buyNos"/>
      <el-table-column  label="产品名称" align="center" prop="goodsName"  show-overflow-tooltip/>
      <el-table-column  label="产品型号" align="center" prop="goodsModel"/>
      <el-table-column label="存货编码" align="center" prop="goodsCode"/>
      <el-table-column label="单价/吨价" align="center" prop="buyDetail.goodsBuyPrice"/>
      <el-table-column label="吨位" align="center" prop="buyDetail.dun"/>
      <el-table-column label="数量" align="center" prop="buyDetail.shengyu"/>
      <el-table-column label="源单金额" align="center" prop="buyDetail.goodsBuyTotal"/>
      <el-table-column label="付款金额" align="center" prop="money"/>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {accAdd, addAction, getAction, getById, updateAction} from '@/api/manage';
import CaigouDetaiList from "@/views/develop/lvBuy/module/CaigouDetailList.vue";
import BuyGoodsList from "@/views/develop/payApply/module/BuyGoodsList.vue";
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";

export default {
  name: "PayRecordDetail",
  dicts:["apply_pay_status","apply_assess_status","pay_record_status"],
  components: {FileUploadShow, BuyGoodsList, CaigouDetaiList},
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      buyDetailList:[],
      payApplyBuyList:[],
      payApplyBuyDetailList:[],
      buyId:null,
      payRecordId:null,
      // 总条数
      total: 0,
      // 查询参数
      buy: {},
      // 表单校验
      rules: {},
      url: {
        getById: '/payRecord/payRecord/',
        add: '/payRecord/payRecord/payAdd',
        edit: '/payRecord/payRecord/edit',
        listByPayRecord: '/payApplyBuyDetail/payApplyBuyDetail/listByPayRecord',
      },
      CS: {
        'text-align': 'center',  //文本居中
        'min-width': '250px',   //最小宽度
        'word-break': 'break-all'  //过长时自动换行
      },
      LS: {
        'color': '#000',
        'text-align': 'center',
        'font-weight': '600',
        'height': '40px',
        'min-width': '110px',
        'word-break': 'keep-all'
      }
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyId: null,
        supplierId: null,
        supplierName: null,
        bankName: null,
        bankNo: null,
        accountNo: null,
        payTime: null,
        money: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },

    showDetail(row) {
      this.open = true;
      const id = row.id;
      this.payRecordId = row.id;
      this.getList();
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "付款记录详情";
      });
    },

    showDetailByRecordId(recordId) {

      this.open = true;
      this.payRecordId = recordId;
      this.getList();
      getById(this.url.getById, recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "付款记录详情";
      });
    },




    getList(){
      this.loading = true;
      let params = {
        pageSizeZero:true,
        "params['payRecordId']": this.payRecordId
      };
      getAction(this.url.listByPayRecord, params).then(response => {
        this.payApplyBuyDetailList = response.rows;
        this.loading = false;
      });
    },

    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        let values ;
        if(column.property === "buyDetail.goodsBuyTotal"){
          values = data.map((item) => Number(item.buyDetail.goodsBuyTotal));
        }else if(column.property === "buyDetail.shengyu"){
          values = data.map((item) => Number(item.buyDetail.shengyu));
        }else{
          values = data.map((item) => Number(item[column.property]));
        }
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "buyDetail.shengyu" || column.property === "buyDetail.goodsBuyTotal" || column.property === "money") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev , curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },



  }
};
</script>

